package com.imessage.service.impl;

import com.bean.*;
import com.imessage.service.BaseService;
import com.imessage.service.ConfigService;
import com.imessage.service.VirtualService;
import com.imessage.service.WumaService;
import com.imessage.type.*;
import io.ebean.Expr;
import io.ebean.Expression;
import io.ebean.Query;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class VirtualServiceImpl implements VirtualService {

    @Autowired
    private BaseService baseService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private WumaService wumaService;

    @Override
    public DeviceVirtual findFinishVirtual(DeviceClient deviceClient) {
        List<Expression> expressionList = new ArrayList<Expression>();
        expressionList.add(Expr.eq("taskCmd", BusinessTaskCmd.send_batch_msg.getCode()));
        expressionList.add(Expr.eq("taskState", BusinessTaskState.finish.getCode()));
        expressionList.add(Expr.ne("state", DeviceVirtualState.delete.getCode()));
        expressionList.add(Expr.eq("DeviceClient.id", deviceClient.getId()));
        return baseService.findObject(DeviceVirtual.class, expressionList);
    }

    @Override
    public long getRunVirtualCount(DeviceClient deviceClient) {
        List<Expression> expressionList = new ArrayList<Expression>();
        expressionList.add(Expr.eq("taskState", BusinessTaskState.run.getCode()));
        expressionList.add(Expr.eq("DeviceClient.id", deviceClient.getId()));
        Query<DeviceVirtual> query=baseService.createQuery(DeviceVirtual.class, expressionList);
        return query.findCount();
    }

    @Override
    public DeviceVirtual getNewDeviceVirtual(DeviceClient deviceClient) {
        BusinessWuma businessWuma=wumaService.findBusinessWumaByRandom();
        if(businessWuma==null){
            return null;
        }
        DeviceVirtual deviceVirtual=new DeviceVirtual();
        deviceVirtual.setDeviceClient(deviceClient);
        deviceVirtual.setState(DeviceVirtualState.shutdown);
        deviceVirtual.setTaskCmd(BusinessTaskCmd.other);
        deviceVirtual.setTaskState(BusinessTaskState.finish);
        deviceVirtual.setVirtualMac(businessWuma.getMac());
        deviceVirtual.setVirtualName(businessWuma.getMac());
        baseService.saveObject(deviceVirtual);
        businessWuma.setState(BusinessWumaState.used);
        baseService.updateObject(businessWuma);
        return deviceVirtual;
    }

    @Override
    public DeviceVirtual findNeedRunVirtual(DeviceClient deviceClient) {
        List<Expression> expressionList = new ArrayList<Expression>();
        expressionList.add(Expr.eq("taskState", BusinessTaskState.finish.getCode()));
        expressionList.add(Expr.eq("taskCmd", BusinessTaskCmd.copy_modify_virtual.getCode()));
        expressionList.add(Expr.eq("DeviceClient", deviceClient));
        DeviceVirtual deviceVirtual=baseService.findObject(DeviceVirtual.class, expressionList);
        if(deviceVirtual!=null){
            return deviceVirtual;
        }
        long maxSilenceTime=50*60*60;
        BaseConfig baseConfig=configService.findObject(BaseConfigCode.max_silence_time);
        if(baseConfig!=null&&StringUtils.isNotBlank(baseConfig.getValue())){
            try{
                maxSilenceTime=Long.valueOf(baseConfig.getValue());
            }catch (Exception e){
                log.error("", e);
            }
        }
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.SECOND, -(int)maxSilenceTime);
        expressionList = new ArrayList<Expression>();
        expressionList.add(Expr.eq("taskState", BusinessTaskState.finish.getCode()));
        expressionList.add(Expr.eq("taskCmd", BusinessTaskCmd.send_single_msg.getCode()));
        expressionList.add(Expr.le("updatedTime",calendar.getTime()));
        expressionList.add(Expr.eq("DeviceClient", deviceClient));
        return baseService.findObject(DeviceVirtual.class, expressionList);
    }

    @Override
    public DeviceVirtual findNeedFinishVirtual(DeviceClient deviceClient) {
        List<Expression> expressionList = new ArrayList<Expression>();
        expressionList.add(Expr.eq("taskState", BusinessTaskState.finish.getCode()));
        expressionList.add(Expr.eq("taskCmd", BusinessTaskCmd.send_batch_msg.getCode()));
        expressionList.add(Expr.eq("DeviceClient", deviceClient));
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.SECOND, -300);
        expressionList.add(Expr.le("updatedTime", calendar.getTime()));
        DeviceVirtual deviceVirtual=baseService.findObject(DeviceVirtual.class, expressionList);
        if(deviceVirtual!=null){
            return deviceVirtual;
        }
        return null;
    }


}
